package com.example.asus.zyfypt_tea_307plx.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.asus.zyfypt_tea_307plx.R;
import com.example.asus.zyfypt_tea_307plx.activities.ViewVideoActivity;
import com.example.asus.zyfypt_tea_307plx.adapter.VideoAdapter;
import com.example.asus.zyfypt_tea_307plx.bean.VideoBean;
import com.example.asus.zyfypt_tea_307plx.iface.Listeners;
import com.example.asus.zyfypt_tea_307plx.model.VideoModel;

import java.util.List;

/**
 * Created by asus on 2018/6/13.
 */

public class Fragment32 extends BaseFragment {
    private List<VideoBean> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private VideoAdapter adapter;
    private int page=1;
    private int lastVisibleItemPosition;
    public Fragment32() {

    }
    private Listeners<List<VideoBean>> listener = new Listeners<List<VideoBean>>() {

        @Override
        public void onResponse(List<VideoBean> beanlist) {
            if(page==1)
            {
                list=beanlist;
            }
            else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "Fragment32  52失败："+msg, Toast.LENGTH_SHORT).show();
            System.out.println(msg);
        }
    };

    //创建时调用
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment32,container,false);
    }

    //触发时调用
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--Fragment32  68--"+getSessionId());
        initRecyclerView(view);
        VideoModel model=new VideoModel();
        model.getResultList("video",1,getSessionId(),listener,1);
    }

    private void initRecyclerView(View view) {
        recyclerView=view.findViewById(R.id.recyclerview32);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
        //实例化适配器
        adapter=new VideoAdapter(context);
        VideoAdapter.OnItemClickListener onItemClickListener= new VideoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                System.out.println("Fragment32--->84");
                Intent intent=new Intent(getActivity(), ViewVideoActivity.class);
                intent.putExtra("videopath",list.get(position).getVideopath());
                System.out.println("Fragment32--->87");
                startActivity(intent);
                System.out.println("Fragment32--->89");
                Toast.makeText(context, "click video31 position:"+position, Toast.LENGTH_SHORT).show();
                System.out.println("Fragment32--->91");
            }
        };

        adapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size())    {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    VideoModel Model = new VideoModel();
                    Model.getResultList("video", page, getSessionId(), listener,1);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });

    }
}